package cn.xfdzx.android.apps.shop.fragment;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.luck.picture.lib.tools.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.HomeSearchActivity;
import cn.xfdzx.android.apps.shop.activity.LocationActivity;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.activity.MessageActivity;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;

import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.VersionInfoBean;
import cn.xfdzx.android.apps.shop.contract.VersionInfoContract;
import cn.xfdzx.android.apps.shop.evenbus.CarPositionMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeAdressMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeTypeMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateCarNumMessage;
import cn.xfdzx.android.apps.shop.fragment.home.HomeCommunityRetailFragment;
import cn.xfdzx.android.apps.shop.fragment.home.HomeMarketPurchaseFragment;
import cn.xfdzx.android.apps.shop.presenter.AddressListPresenter;
import cn.xfdzx.android.apps.shop.presenter.VersionInfoPresenter;
import cn.xfdzx.android.apps.shop.util.ColorShades;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.IArcView;
import cn.xfdzx.android.apps.shop.widget.CommonProgressDialog;
import cn.xfdzx.android.apps.shop.widget.UpdateVersionDialog;

/**
 * @author gzk
 * @date 2020/4/29 0029
 * description：
 */
public class OneFragment extends BaseMvpFragment<VersionInfoPresenter> implements View.OnClickListener, VersionInfoContract.View {


    LinearLayout llState;
    RelativeLayout rlTop;
    LinearLayout llHomeSearch;
    LinearLayout llHomeLocation;
    ViewPager vpMainOne;
    TabLayout mTabLayout;
    TextView tvHomeAddress;
    LinearLayout llTitle;
    IArcView iavBg;
    ImageView ivMessage;
    private String[] title = {"社区零售", "新发地市场采购"};
    private HomeVPAdapter homeVPAdapter;
    private List<Fragment> mFragments;
    private LinearLayout.LayoutParams item_bar;

    private ColorShades shades;
    private String[] colorBg = {"#fb9129", "#02C557"};
//    public static int TYPE_IS_RETAIL = 0;//当前下标是否在零售

    private String addressMessage="";//当前地址

    @Override
    protected void initView(View view) {
        mPresenter = new VersionInfoPresenter();
        mPresenter.attachView(this);
        llState = view.findViewById(R.id.ll_state);
        rlTop = view.findViewById(R.id.rl_top);
        llHomeSearch = view.findViewById(R.id.ll_home_search);
        llHomeLocation = view.findViewById(R.id.ll_home_location);
        vpMainOne = view.findViewById(R.id.vp_main_one);
        mTabLayout = view.findViewById(R.id.tl_home_title);
        tvHomeAddress = view.findViewById(R.id.tv_home_address);
        llTitle = view.findViewById(R.id.ll_title);
        iavBg = view.findViewById(R.id.iav_bg);
        ivMessage = view.findViewById(R.id.iv_message);
        ivMessage.setOnClickListener(this);
        llHomeSearch.setOnClickListener(this);
        llHomeLocation.setOnClickListener(this);

        EventBus.getDefault().register(this);
        initHigh();
        init();

    }

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_one;
    }

    private void init() {
        shades = new ColorShades();
        mFragments = new ArrayList<>();
        mFragments.add(HomeCommunityRetailFragment.newInstance());//社区零售
        mFragments.add(HomeMarketPurchaseFragment.newInstance());//市场采购
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.picture_color_transparent)));

        for (String tab : title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tab));
        }
        homeVPAdapter = new HomeVPAdapter(getChildFragmentManager(), title, mFragments);
        vpMainOne.setAdapter(homeVPAdapter);

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.item_textview);
                }
                TextView textView = tab.getCustomView().findViewById(R.id.tv_item_text);
                textView.setTextSize(18);
                textView.setText(tab.getText());
                textView.setTextColor(mTabLayout.getTabTextColors());
                textView.setTypeface(Typeface.DEFAULT_BOLD);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.item_textview);
                }
                TextView textView = tab.getCustomView().findViewById(R.id.tv_item_text);
                textView.setTextSize(14);
                textView.setText(tab.getText());
                textView.setTextColor(mTabLayout.getTabTextColors());
                textView.setTypeface(Typeface.DEFAULT);

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mTabLayout.setupWithViewPager(vpMainOne);

        vpMainOne.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                shades.setFromColor(colorBg[position % colorBg.length])
                        .setToColor(colorBg[(position + 1) % colorBg.length])
                        .setShade(positionOffset);
                llTitle.setBackgroundColor(shades.generate());
                llState.setBackgroundColor(shades.generate());
                rlTop.setBackgroundColor(shades.generate());
                iavBg.setToColor(shades.generate());
            }

            @Override
            public void onPageSelected(int position) {
//                TYPE_IS_RETAIL = position;
                if (position == 0) {
                  //  ((MainActivity) getActivity()).setBottom(true);
                } else {
                  //  ((MainActivity) getActivity()).setBottom(false);
                }
                EventBus.getDefault().post(new HomeTypeMessage(position));

                EventBus.getDefault().post(new UpdateCarNumMessage());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initHigh() {
        item_bar = (LinearLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(getActivity());
        llState.setLayoutParams(item_bar);
    }

    //更新地址
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(HomeAdressMessage messageEvent) {
        Log.e("messageEvent",messageEvent.getLng()+"--经纬onefragment度--"+messageEvent.getLat());
        addressMessage = messageEvent.getMessage();
        tvHomeAddress.setText(messageEvent.getMessage());
    }

    //与购物车同步
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXXX(CarPositionMessage messageEvent) {
       vpMainOne.setCurrentItem(messageEvent.getMessage());
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onVersionInfoSuccess(VersionInfoBean bean) {

    }

    public static OneFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        OneFragment fragment = new OneFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_home_location:
                if (Utils.isFastClick()){
                Intent intent = new Intent(getActivity(), LocationActivity.class);
                intent.putExtra("pageForm","first");
                intent.putExtra("send_to_address",addressMessage);
                startActivity(intent);}
                break;
            case R.id.ll_home_search:
                if (Utils.isFastClick()){
                Intent home_search = new Intent(getActivity(), HomeSearchActivity.class);

                startActivity(home_search);}
                break;
            case R.id.iv_message:
                //跳转消息
                Intent intent = new Intent(getActivity(), MessageActivity.class);
                startActivity(intent);
                break;
        }
    }


}
